package com.service.weChatWifiLogin.service.impl;

import com.service.config.constant.Constant;
import com.service.config.utils.IdUtil;
import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.ModelMapperUtil;
import com.service.weChatWifiLogin.dao.ReviewRecordInfoDao;
import com.service.weChatWifiLogin.dto.ReviewInfoDto;
import com.service.weChatWifiLogin.dto.ReviewRecordInfoDto;
import com.service.weChatWifiLogin.dto.ShopInfoReviewDto;
import com.service.weChatWifiLogin.entity.ReviewRecordInfo;
import com.service.weChatWifiLogin.entity.ShopInfoReview;
import com.service.weChatWifiLogin.dao.ShopInfoReviewDao;
import com.service.weChatWifiLogin.entity.UserInfo;
import com.service.weChatWifiLogin.entity.VFilePhysicsInfo;
import com.service.weChatWifiLogin.service.ShopCustomerService;
import com.service.weChatWifiLogin.service.ShopInfoReviewService;
import com.service.weChatWifiLogin.service.VFilePhysicsInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.service.weChatWifiLogin.utile.MD5Util.getUrl;

/**
 * 用户提交后需要审核的表(ShopInfoReview)表服务实现类
 *
 * @author makejava
 * @since 2021-06-29 14:42:43
 */
@Service("shopInfoReviewService")
public class ShopInfoReviewServiceImpl implements ShopInfoReviewService {
    @Resource
    private ShopInfoReviewDao shopInfoReviewDao;
    @Resource
    private ReviewRecordInfoDao reviewRecordInfoDao;
    @Resource
    private VFilePhysicsInfoService vFilePhysicsInfoService;
    @Resource
    private ShopCustomerService shopCustomerService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ShopInfoReview queryById(String id) {
        return this.shopInfoReviewDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ShopInfoReview> queryAllByLimit(int offset, int limit) {
        return this.shopInfoReviewDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param shopInfoReview 实例对象
     * @return 实例对象
     */
    @Override
    public ShopInfoReview insert(ShopInfoReview shopInfoReview) {
        this.shopInfoReviewDao.insert(shopInfoReview);
        return shopInfoReview;
    }

    /**
     * 修改数据
     *
     * @param shopInfoReview 实例对象
     * @return 实例对象
     */
    @Override
    public ShopInfoReview update(ShopInfoReview shopInfoReview) {
        this.shopInfoReviewDao.update(shopInfoReview);
        return this.queryById(shopInfoReview.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.shopInfoReviewDao.deleteById(id) > 0;
    }

    @Override
    public List<ShopInfoReviewDto> queryAll(ShopInfoReview shopInfoReview,String flag) {
        List<ShopInfoReview> shopInfoReviewList = new ArrayList<>();
        if (flag.equals("1")) { // 查询提交审核的店铺
            shopInfoReview.setReviewStatus(Constant.REVIEW_TIME);
            shopInfoReviewList = shopInfoReviewDao.queryAll(shopInfoReview);
        }else if (flag.equals("2")){
            // 查询审核成功和审核失败的店铺
            List<Integer> list = new ArrayList<>();
            list.add(2);
            list.add(3);
            shopInfoReviewList = shopInfoReviewDao.queryByReviewStatus(list);
        }else {
            // 查询所有
            shopInfoReviewList = shopInfoReviewDao.queryAll(shopInfoReview);
        }
        List<ShopInfoReviewDto> shopInfoReviewDtos = ModelMapperUtil.strictMapList(shopInfoReviewList, ShopInfoReviewDto.class);
        shopInfoReviewList.forEach(shopInfoReview1 -> {
            shopInfoReviewDtos.forEach(shopInfoReviewDto -> {
                if (shopInfoReview1.getId().equals(shopInfoReviewDto.getId())) {
                    try {
                        if (shopInfoReview1.getComment() != null) {
                            ReviewRecordInfoDto reviewRecordInfoDto = JsonXMLUtils.json2obj(shopInfoReview1.getComment(), ReviewRecordInfoDto.class);
                            shopInfoReviewDto.setComment(reviewRecordInfoDto);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
             });
        });
        shopInfoReviewDtos.forEach(shopInfoReviewDto -> {
            if (shopInfoReviewDto.getLogoType() != null) {
                if (shopInfoReviewDto.getLogoType()==0) {
                    // 设置logo的url
                    VFilePhysicsInfo vFilePhysicsInfo = vFilePhysicsInfoService.queryById(shopInfoReviewDto.getLogoId());
                    if (vFilePhysicsInfo != null) {
                        shopInfoReviewDto.setLogoId(getUrl(vFilePhysicsInfo));
                    }
                }
            }
            if (shopInfoReviewDto.getMarketingType()!=null) {
                if (shopInfoReviewDto.getMarketingType()==0) { // 设置营销图片的url
                    VFilePhysicsInfo vFilePhysicsInfo = vFilePhysicsInfoService.queryById(shopInfoReviewDto.getMarketingId());
                    if (vFilePhysicsInfo != null) {
                        shopInfoReviewDto.setMarketingId(getUrl(vFilePhysicsInfo));
                    }
                }
            }
        });
        return shopInfoReviewDtos;
    }

    /**
     * 审核boss端配置信息
     * @param reviewRecordInfoDtoList
     */
    @Override
    public void verifyShopInfo( String cloudAuthToken,String shopId,String flag,ReviewRecordInfoDto reviewRecordInfoDtoList) throws Exception {
        UserInfo userInfo = JsonXMLUtils.json2obj(cloudAuthToken, UserInfo.class);
        String id = IdUtil.getStringId(); // 获取审批号
        if (flag.equals("1")) { // 审核通过
            ShopInfoReview shopInfoReview = new ShopInfoReview();
            shopInfoReview.setId(shopId);
            reviewRecordInfoDtoList.setVersionNo(id);
            reviewRecordInfoDtoList.setReviewer(userInfo.getUserName());
            shopInfoReview.setReviewStatus(Constant.REVIEW_SUCCESS); // 3--审核成功
            shopInfoReview.setReviewStatusDesc("审核成功"); // 3--审核成功
            List<ReviewRecordInfo> reviewRecordInfos = ModelMapperUtil.strictMapList(reviewRecordInfoDtoList.getReviewInfo(), ReviewRecordInfo.class);
            reviewRecordInfos.forEach(reviewRecordInfo -> {
                reviewRecordInfo.setId(IdUtil.getStringId());
                reviewRecordInfo.setReviewStatus(Constant.REVIEW_SUCCESS); // 2--审核失败
                reviewRecordInfo.setReviewerContent(reviewRecordInfoDtoList.getReviewerContent());
                reviewRecordInfo.setShopId(reviewRecordInfoDtoList.getShopId());
                reviewRecordInfo.setReviewerTime(new Date());
                reviewRecordInfo.setReviewStatus(3);
                reviewRecordInfo.setReviewer(userInfo.getUserName());
                reviewRecordInfo.setVersionNo(id);
            });
            reviewRecordInfoDao.insertBatch(reviewRecordInfos);
            shopInfoReview.setComment(JsonXMLUtils.obj2json(reviewRecordInfoDtoList));
            shopInfoReview.setUpdateTime(new Date());
            shopInfoReviewDao.update(shopInfoReview);
        }else {
            reviewRecordInfoDtoList.setVersionNo(id);
            reviewRecordInfoDtoList.setReviewer(userInfo.getUserName());// 审批单号
            List<ReviewRecordInfo> reviewRecordInfos = ModelMapperUtil.strictMapList(reviewRecordInfoDtoList.getReviewInfo(), ReviewRecordInfo.class);
            List<ReviewRecordInfo> reviewRecordInfoList = reviewRecordInfos.stream().filter(ReviewRecordInfo::getStatus).collect(Collectors.toList());
            if (reviewRecordInfoList.size() > 0) {
                reviewRecordInfos.forEach(reviewRecordInfo -> {
                    reviewRecordInfo.setId(IdUtil.getStringId());
                    reviewRecordInfo.setReviewStatus(2); // 2--审核失败
                    reviewRecordInfo.setReviewerContent(reviewRecordInfoDtoList.getReviewerContent());
                    reviewRecordInfo.setShopId(reviewRecordInfoDtoList.getShopId());
                    reviewRecordInfo.setReviewerTime(new Date());
                    reviewRecordInfo.setReviewStatus(2);
                    reviewRecordInfo.setReviewer(userInfo.getUserName());
                    reviewRecordInfo.setVersionNo(id);
                });
            }else {
                reviewRecordInfos.forEach(reviewRecordInfo -> {
                    reviewRecordInfo.setId(IdUtil.getStringId());
                    reviewRecordInfo.setStatus(true);
                    reviewRecordInfo.setReviewStatus(Constant.REVIEW_FAIL); // 2--审核失败
                    reviewRecordInfo.setReviewerContent(reviewRecordInfoDtoList.getReviewerContent());
                    reviewRecordInfo.setShopId(reviewRecordInfoDtoList.getShopId());
                    reviewRecordInfo.setReviewerTime(new Date());
                    reviewRecordInfo.setReviewer(userInfo.getUserName());
                    reviewRecordInfo.setVersionNo(id);
                });
            }
            reviewRecordInfoDtoList.setReviewInfo(ModelMapperUtil.strictMapList(reviewRecordInfos,ReviewInfoDto.class));
            reviewRecordInfoDao.insertBatch(reviewRecordInfos);
            ShopInfoReview shopInfoReview = new ShopInfoReview();
            shopInfoReview.setId(shopId);
            shopInfoReview.setReviewStatus(Constant.REVIEW_FAIL); // 2--审核失败
            shopInfoReview.setReviewStatusDesc("审核失败");
            shopInfoReview.setUpdateTime(new Date());
            shopInfoReview.setComment(JsonXMLUtils.obj2json(reviewRecordInfoDtoList));
            shopInfoReviewDao.update(shopInfoReview);
        }
    }
}
